package com.cn.sl.server.common.dto

import com.querydsl.core.types.Path
import com.querydsl.core.types.dsl.BooleanExpression
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

interface RangeAble<T : Comparable<*>, F : Path<T>> {
    val min: T?
    val max: T?

    fun between(fieldPath: F): BooleanExpression
}

@OptIn(ExperimentalContracts::class)
fun <T : Comparable<*>, F : Path<T>> RangeAble<T, F>?.act(fieldPath: F, action: (BooleanExpression) -> Unit) {
    if (!isNull() && (min != null || max != null)) {
        action(between(fieldPath))
    }
}

@OptIn(ExperimentalContracts::class)
fun <T : Comparable<*>, F : Path<T>> RangeAble<T, F>?.isNull(): Boolean {
    contract {
        returns(false) implies (this@isNull != null)
    }
    return this == null
}